package com.woniu.his.cms.app.apiImpl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.injector.methods.UpdateById;
import com.woniu.his.cms.client.NonDrugModelClient;
import com.woniu.his.cms.client.param.DmsNonDrugModelParam;
import com.woniu.his.cms.client.param.NonDrugModelQueryParam;
import com.woniu.his.cms.client.param.UpdateNonDrugModelParam;
import com.woniu.his.cms.client.result.CreateNonDrugModelResult;
import com.woniu.his.cms.client.result.QueryDrugModelResult;
import com.woniu.his.cms.common.CmsResultCode;
import com.woniu.his.cms.infra.model.NonDrug;
import com.woniu.his.cms.infra.model.NonDrugItemRecord;
import com.woniu.his.cms.infra.model.NonDrugModel;
import com.woniu.his.cms.infra.model.NonDrugRelation;
import com.woniu.his.cms.service.NonDrugItemRecordService;
import com.woniu.his.cms.service.NonDrugModelService;
import com.woniu.his.cms.service.NonDrugRelationService;
import com.woniu.his.cms.service.dto.CreateNonDrugModelDto;
import com.woniu.his.cms.service.dto.QueryNonDrugModelDto;
import com.woniu.his.cms.service.param.NonDrugModelSvParam;
import com.woniu.his.cms.service.param.QueryNonDrugModelSvParam;
import com.woniu.his.cms.service.param.UpdateNonDrugModelSvParam;
import com.woniu.his.exception.HisException;
import com.woniu.his.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;


@RestController
@Slf4j
@RequestMapping("/cms")
public class NonDrugModelApiImpl implements NonDrugModelClient {

    @Resource
    private NonDrugModelService nonDrugModelService;

    @Resource
    private NonDrugItemRecordService nonDrugItemRecordService;

    @Resource
    private NonDrugRelationService nonDrugRelationService;


    /**
     * 描述：创建模版
     */
    @Override
    public Result<CreateNonDrugModelResult> createNonDrugModel(DmsNonDrugModelParam dmsNonDrugModelParam) {

        log.info("新增加非药品模板参数:{}", dmsNonDrugModelParam);
        NonDrugModelSvParam param = BeanUtil.copyProperties(dmsNonDrugModelParam, NonDrugModelSvParam.class);
        CreateNonDrugModelDto createNonDrugModelDto = nonDrugModelService.createNonDrugModel(param);
        CreateNonDrugModelResult result = BeanUtil.copyProperties(createNonDrugModelDto, CreateNonDrugModelResult.class);
        return Result.getInstance(CreateNonDrugModelResult.class).setData(result).setMsg("新增非药品模板成功");
    }


    /**
     * 删除模板 是否需要事务管理   @GlobalTransactional ???
     *
     * @param ids
     * @return
     */
    @Override
    public Result deleteNonDrugModel(List<Long> ids) {
        log.info("删除非药品模板参数:{}", ids);
        boolean a = false;
        for (Long id : ids) {
            NonDrugModel nonDrugModel = nonDrugModelService.getById(id);
            if (nonDrugModel == null) {
                throw new HisException(CmsResultCode.NON_DRUG_MODEL_NOT_EXITS);
            }
            if (nonDrugModel.getStatusNdm().equals("OFF")) {
                throw new HisException(CmsResultCode.NON_DRUG_MODEL_DELETED);
            }
            //被使用中的模板暂时不能删除
            List<NonDrugRelation> list1 = nonDrugRelationService.list(new QueryWrapper<NonDrugRelation>().eq("non_drug_model_id", id));
            if (list1.size()>0) {
                list1.forEach(o -> {
                    Long nonDrugId = o.getNonDrugId();
                    System.out.println(nonDrugId);
                    List<NonDrugItemRecord> list = nonDrugItemRecordService.list(new QueryWrapper<NonDrugItemRecord>().eq("no_drug_id",
                            nonDrugId).in("non_drug_item_status", "NOT_PAY", "PAID", "NOT_REGISTERED", "REGISTERED"));
                    System.out.println(list);
                    if (list.size() > 0) throw new HisException(CmsResultCode.PATIENT_NEED_TO_CHECK);
                });
            }
        }
        for (Long id : ids) {
            NonDrugModel nonDrugModel = nonDrugModelService.getById(id);
            nonDrugModel.setStatusNdm("OFF");
            a = nonDrugModelService.updateById(nonDrugModel);
            List<NonDrugRelation> list1 = nonDrugRelationService.list(new QueryWrapper<NonDrugRelation>().eq("non_drug_model_id", id));
            list1.forEach(c -> nonDrugRelationService.removeById(c.getId()));
        }
        return a ? Result.getInstance().setMsg("删除非药品模板及关联表成功") : Result.getInstance().setMsg("删除非药品模板及关联表失败");
    }


    /**
     * @Description: 更新模板
     * @Param:
     * @return:
     * @Date: 2022/11/30
     */
    @Override
    public Result<CreateNonDrugModelResult> updateNonDrugModel(UpdateNonDrugModelParam param2) {

        log.info("修改加非药品模板参数:{}", param2);
        UpdateNonDrugModelSvParam param = BeanUtil.copyProperties(param2, UpdateNonDrugModelSvParam.class);
        CreateNonDrugModelDto createNonDrugModelDto = nonDrugModelService.updateModel(param);
        CreateNonDrugModelResult result = BeanUtil.copyProperties(createNonDrugModelDto, CreateNonDrugModelResult.class);
        return Result.getInstance(CreateNonDrugModelResult.class).setData(result).setMsg("修改非药品模板成功");

    }

    /**
     * @Description: 多提交查询模板
     * @Param:
     * @return:
     * @Date: 2022/11/30
     */
    @Override
    public Result<List<QueryDrugModelResult>> selectNonDrugModel(DmsNonDrugModelParam queryParam) {
        log.info("查询加非药品模板参数:{}", queryParam);

        QueryNonDrugModelSvParam param = BeanUtil.copyProperties(queryParam, QueryNonDrugModelSvParam.class);
        List<QueryNonDrugModelDto>  list = nonDrugModelService.selectNonDrugModel(param);
        List<QueryDrugModelResult> result = BeanUtil.copyToList(list, QueryDrugModelResult.class);
        return Result.getInstanceList(QueryDrugModelResult.class).setData(result).setMsg("查询非药品模板成功");
    }





}
